const amqp = require('amqp')

const options = {
  host: '127.0.0.1',
  port: 5673,
  login: 'guest',
  password: 'hellojessica',
  connectionTimeout: 1000,
  vhost: '/',
  noDelay: true,
  heartbeat: 60, 
}

const mqUrl  = { url: 'amqp://guest:hellojessica@127.0.0.1:5673' }

const connection = amqp.createConnection(options)

connection.on('error', async (err) => {
  console.log(`############ MQ CONN ERROR ON ${new Date()} ############`, err)
})

connection.on('ready', () => {
  // queue
  connection.queue(sails.config.CRM_MQ_ACCOUNT.QUEUE_NAME, { durable: true }, (queue) => {
    queue.bind('hello.world.exchange', 'hello.world.route')
    queue.subscribe({ ack: true, prefetchCount: 1 }, 
      async (message, headers, deliveryInfo, ack) => {
      console.log('MQ format message data', message)
      ack.acknowledge(true)
    })
  })
})


process.on('exit', (code) => {
  console.log('MQ DISCONECT')
  
  connection.disconnect()  

})
